
package com.kanjian.music.fragment;

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.toolbox.ImageRequest;
import com.kanjian.music.R;
import com.kanjian.music.activity.BaseActivity;
import com.kanjian.music.dialog.ShareDialog;
import com.kanjian.music.entity.Music;
import com.kanjian.music.util.PlayerUtil;
import com.kanjian.music.view.RecyclingImageView;
import com.kanjian.music.volley.VolleyQueue;

public class MusicPlayerMusicInfoFragment extends BaseFragment {

    public static MusicPlayerMusicInfoFragment newInstance() {
        return new MusicPlayerMusicInfoFragment();
    }

    private View mRootView;

    private RecyclingImageView mIv_musicPic;

    private TextView mTv_musicName;

    private TextView mTv_musicianName;

    private Button mBtn_playMode;

    private Button mBtn_next;

    private Button mBtn_pre;
    
    private Button mBtn_share;

    private SeekBar mSb_progress;

    private boolean mIsTouching;

    private TextView mTv_musicTotalTime;

    private TextView mTv_musicPlayProgress;

    private Music mMusic;
    
    private boolean mIsShowing;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        initView(inflater, container);
        return mRootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mRootView = null;
    }
    
    @Override
    public void onResume() {
        super.onResume();
        mIsShowing = true;
    }
    
    @Override
    public void onPause() {
        super.onPause();
        mIsShowing = false;
    }

    private void initView(LayoutInflater inflater, ViewGroup container) {
        if (mRootView == null) {
            mRootView = inflater.inflate(R.layout.fragment_musicplayer_musicinfo, container, false);
            mIv_musicPic = (RecyclingImageView) mRootView
                    .findViewById(R.id.music_player_music_info_pic);
            mTv_musicName = (TextView) mRootView
                    .findViewById(R.id.music_player_music_info_music_name);
            mTv_musicianName = (TextView) mRootView
                    .findViewById(R.id.music_player_music_info_musician_name);
            mBtn_playMode = (Button) mRootView.findViewById(R.id.music_player_music_info_play_mode);
            mBtn_playMode.setText(PlayerUtil.getPlayModeStr());
            mBtn_playMode.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    mBtn_playMode.setText(PlayerUtil.togglePlayMode());
                }
            });
            mBtn_next = (Button) mRootView.findViewById(R.id.music_player_music_controler_next_btn);
            mBtn_next.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    ((BaseActivity) getActivity()).mPlayService.next(true);
                }
            });
            mBtn_pre = (Button) mRootView.findViewById(R.id.music_player_music_controler_pre_btn);
            mBtn_pre.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    ((BaseActivity) getActivity()).mPlayService.pre();
                }
            });
            mBtn_share = (Button) mRootView.findViewById(R.id.music_player_music_info_share);
            mBtn_share.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    ShareDialog.newInstance(mMusic).show(getFragmentManager(), "");
                }
            });
            mSb_progress = (SeekBar) mRootView.findViewById(R.id.music_player_music_info_seekbar);
            mSb_progress.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {
                    mIsTouching = false;
                    ((BaseActivity) getActivity()).mPlayService.seek(seekBar.getProgress());
                }

                @Override
                public void onStartTrackingTouch(SeekBar seekBar) {
                    mIsTouching = true;
                }

                @Override
                public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                }
            });
            mTv_musicTotalTime = (TextView) mRootView
                    .findViewById(R.id.music_player_music_info_total_time);
            mTv_musicPlayProgress = (TextView) mRootView
                    .findViewById(R.id.music_player_music_info_played_time);
            refreshMusic();
        }
    }

    public void refreshMusic() {
        if (!mIsShowing) {
            return;
        }
        mMusic = PlayerUtil.getCurrentPlayingMusic();
        if (mMusic.getPlayTime() == null) {
            mTv_musicTotalTime.setText("--");
        } else {
            mTv_musicTotalTime.setText(PlayerUtil.toTime(mMusic.getPlayTime()));
        }
        if (mMusic != null) {
            mTv_musicName.setText(mMusic.getMusicName());
            mTv_musicianName.setText(mMusic.getUserName());
            VolleyQueue.getImageRequestQueue().add(
                    new ImageRequest(mMusic.getUserPic(), new Response.Listener<Bitmap>() {
                        @Override
                        public void onResponse(Bitmap bmp) {
                            if (mIv_musicPic != null) {
                                mIv_musicPic.setImageBitmap(bmp);
                            }
                        }
                    }, 0, 0, Config.ARGB_8888, null));
        }
    }

    public void onProgress(int percent, int playTime) {
        if (!mIsShowing) {
            return;
        }
        if (!mIsTouching) {
            mSb_progress.setProgress(percent);
        }
        mTv_musicPlayProgress.setText(PlayerUtil.toTime(playTime));
    }

}
